package com.inforich.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.Arrays;
import cn.hutool.core.bean.BeanUtil;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;
import com.inforich.common.annotation.Log;
import com.inforich.common.core.controller.BaseController;
import com.inforich.common.core.domain.AjaxResult;
import com.inforich.common.enums.BusinessType;
import com.inforich.system.domain.SysRubbishType;
import com.inforich.system.service.ISysRubbishTypeService;
import com.inforich.common.utils.poi.ExcelUtil;

/**
 * 系统垃圾类型表Controller
 *
 * @author hjm
 * @date 2023-07-04
 */
@RestController
@RequestMapping("/system/rubbishType")
public class SysRubbishTypeController extends BaseController {

  @Autowired
  private ISysRubbishTypeService sysRubbishTypeService;

  /**
   * 根据父ID查询
   */
  @GetMapping("/listByParentId")
  public AjaxResult listByParentId(String parentId) {
    List<SysRubbishType> list = sysRubbishTypeService.list(new LambdaQueryWrapper<SysRubbishType>()
        .eq(SysRubbishType::getParentId, parentId));
    return AjaxResult.success(list);
  }

  /**
   * 查询系统垃圾类型表列表
   */
  @PreAuthorize("@ss.hasPermi('system:rubbishType:list')")
  @GetMapping("/list")
  public AjaxResult list(SysRubbishType sysRubbishType) {
    List<SysRubbishType> list = sysRubbishTypeService.list();
    return AjaxResult.success(list);
  }

  /**
   * 导出系统垃圾类型表列表
   */
  @PreAuthorize("@ss.hasPermi('system:rubbishType:export')")
  @Log(title = "系统垃圾类型表", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  public void export(HttpServletResponse response, SysRubbishType sysRubbishType) {
    Map<String, Object> map = beanToMap(sysRubbishType);
    List<SysRubbishType> list = sysRubbishTypeService.listSysRubbishType(map);
    ExcelUtil<SysRubbishType> util = new ExcelUtil<SysRubbishType>(SysRubbishType.class);
    util.exportExcel(response, list, "系统垃圾类型表数据");
  }

  /**
   * 获取系统垃圾类型表详细信息
   */
  @PreAuthorize("@ss.hasPermi('system:rubbishType:query')")
  @GetMapping(value = "/{id}")
  public AjaxResult getInfo(@PathVariable("id") Long id) {
    SysRubbishType sysRubbishType = sysRubbishTypeService.getById(id);
    return AjaxResult.success(sysRubbishType);
  }

  /**
   * 新增系统垃圾类型表
   */
  @PreAuthorize("@ss.hasPermi('system:rubbishType:add')")
  @Log(title = "系统垃圾类型表", businessType = BusinessType.INSERT)
  @PostMapping
  public AjaxResult add(@Validated @RequestBody SysRubbishType sysRubbishType) {
    return toAjax(sysRubbishTypeService.save(sysRubbishType));
  }

  /**
   * 修改系统垃圾类型表
   */
  @PreAuthorize("@ss.hasPermi('system:rubbishType:edit')")
  @Log(title = "系统垃圾类型表", businessType = BusinessType.UPDATE)
  @PutMapping
  public AjaxResult edit(@Validated @RequestBody SysRubbishType sysRubbishType) {
    return toAjax(sysRubbishTypeService.updateById(sysRubbishType));
  }

  /**
   * 删除系统垃圾类型表
   */
  @PreAuthorize("@ss.hasPermi('system:rubbishType:remove')")
  @Log(title = "系统垃圾类型表", businessType = BusinessType.DELETE)
  @DeleteMapping("/{ids}")
  public AjaxResult remove(@PathVariable Long[] ids) {
    return toAjax(sysRubbishTypeService.removeByIds(Arrays.asList(ids)));
  }
}
